<?php

require_once(Yii::getPathOfAlias('webroot')."/generate.php");

class CategoriesThemeController extends Controller
{
	public function filters()
	{
		return array(
			'rights', // perform access control for CRUD operations
		);
	}
	public function allowedActions()
	{
		//return 'error';
	}
	
	public function actionIndex()
	{
		$model = new CategoriesTheme('search');
		$model->unsetAttributes();  // clear any default values
		
		if(isset($_GET['CategoriesTheme']))
			$model->attributes=$_GET['CategoriesTheme'];

		$this->render('index',array(
			'model'=>$model,
		));
	}
	public function actionCreate()
	{
		$model = new CategoriesTheme; 
		if(!empty($_POST['CategoriesTheme'])){
			$model->attributes = $_POST['CategoriesTheme'];
			$model->created = time();
			$model->alias = alias($_POST['CategoriesTheme']['name']);
			$model->alias_folder = alias_change($_POST['CategoriesTheme']['alias_folder']);
			
			if($model->save()){
			
				// Create folder
				$generate = new generate;
				$generate->createFolder($model->alias_folder, "/shop/");
				$generate->createFolder("db", "/shop/".$model->alias_folder."/");
				$generate->createFolder("theme", "/shop/".$model->alias_folder."/");
				
				Yii::app()->user->setFlash('success', translate('Thêm lĩnh vực theme thành công.'));
				$this->redirect(PIUrl::createUrl('/admin/categoriesTheme/index'));
			}
		}
		
		$this->render('create', array("model"=>$model));
	}
	
	public function actionUpdate($id = null)
	{
		$model = CategoriesTheme::model()->findByPk($id); 
		$alias_folder_old = $model->alias_folder;

		if(!empty($_POST['CategoriesTheme'])){
		
			$model->attributes = $_POST['CategoriesTheme'];
			$model->updated = time();
			$model->alias_folder = alias_change($_POST['CategoriesTheme']['alias_folder']);
			
			if($model->save()){
				
				// rename alias folder
				if($alias_folder_old != $_POST['CategoriesTheme']['alias_folder']){
					$path = Yii::getPathOfAlias('webroot')."/shop/".$alias_folder_old;
					$pathNew = Yii::getPathOfAlias('webroot')."/shop/".$model->alias_folder;
					rename($path, $pathNew);
				}
				
				Yii::app()->user->setFlash('success', translate('Cập nhập lĩnh vực theme thành công.'));
				$this->redirect(PIUrl::createUrl('/admin/categoriesTheme/index'));
			}
		}
		
		$this->render('update', array("model"=>$model));
	}
	
	public function loadModel($id)
	{
		$model=CategoriesTheme::model()->findByPk($id);
		if($model===null)
			throw new CHttpException(404,'The requested page does not exist.');
		return $model;
	}
	
	public function actionDelete($id)
	{
		$generate = new generate;
		
		$model = $this->loadModel($id);
		// remove alias folder
		$path = Yii::getPathOfAlias('webroot')."/shop/".$model->alias_folder;
		if(is_dir($path)){
			$generate->rrmdir($path);
		}
		$model->delete();

		// if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
		if(!isset($_GET['ajax']))
			$this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('index'));
	}
	// function delete rows
	public function actionDeleteAll($id){
		$arrIdNew = explode(",",$id);
		$generate = new generate;
		for($i=0; $i<count($arrIdNew); $i++){
			$model = $this->loadModel($arrIdNew[$i]);
			$path = Yii::getPathOfAlias('webroot')."/shop/".$model->alias_folder;
			if(is_dir($path)){
				$generate->rrmdir($path);
			}
			$model->delete();
		}
		if(!isset($_GET['ajax']))
			$this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('index'));
	}
}